﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Plotter
{
    class DrawableObject
    {
        public Point[] Points { get; set; }
        public Point[] MappedPoints { get; set; }

        public List<PlotterLine> XPlotterLines { get; set; }
        public List<PlotterLine> YPlotterLines { get; set; }
        public double ScewFactor { get; set; }

        public DrawableObject(Point[] points, double scew_factor)
        {
            this.Points = points;
            this.ScewFactor = scew_factor;

            this.XPlotterLines = new List<PlotterLine>();
            this.YPlotterLines = new List<PlotterLine>();
        }

        private void CalculatePlotterLines()
        {
            for (int i = 0; i < this.Points.Length; i++)
            {
                Point current = this.MappedPoints[i];
                Point next = this.MappedPoints[(i + 1) % this.MappedPoints.Length];

                int original_dx = next.X - current.X;
                int original_dy = next.Y - current.Y;
                int dx = Math.Abs(next.X - current.X);
                int dy = Math.Abs(next.Y - current.Y);

                double v_x, v_y;
                if (dx < dy)
                {
                    v_x = (double)dx / (double)dy;
                    v_y = 1.0;
                }
                else
                {
                    v_x = 1.0;
                    v_y = (double)dy / (double)dx;
                }

                this.XPlotterLines.Add(new PlotterLine(original_dx, v_x));
                this.YPlotterLines.Add(new PlotterLine(original_dy, v_y));
            }
        }

        public void MapPositions(Plotter plotter)
        {
            PositionMapper mapper = new PositionMapper(plotter.StepWidth, plotter.StepHeight, this.ScewFactor);
            this.MappedPoints = mapper.Map(this.Points);

            this.CalculatePlotterLines();
        }
    }
}
